//logs.js
const util = require('../../utils/util.js');
const http = util.http;

Page({
  data: {
    navbar: ['他人发布', '我的接单', '我发布的'],
    currentTab: 0,
    isTake: false,
    isChoose: false,
    showDialog: false,
    nickname: '',
    param: {
      page: 1,
      pagesize: 10,
    },
    total: 0,
    list: [],
    myList: [],
    myPublish: [],
    name: '',  // 接单人名字
    phone: '', // 接单人电话
    project_id: '',
    choose: [],
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx,
      TypeItem: this.data.navbar[this.data.currentTab],
      param: {
        page: 1,
        pagesize: 10,
        total: 0,
      }
    })
    if (this.data.currentTab == 1) {
      this.setData({
        myList: []
      })
      this.getMyList();
    } else if (this.data.currentTab == 2) {
      this.setData({
        myPublish: []
      })
      this.getMyPublish();
    } else {
      this.setData({
        list: []
      })
      this.getList();
    }
  },
  handleAccept: function (e) {
    const { id } = e.currentTarget.dataset;
    this.setData({
      isTake: true,
      project_id: id,
    });
  },
  getTakerInfo: function (e) {
    var type = e.target.dataset.type;
    this.setData({
      [type]: e.detail.value
    })
  },
  confirmBusiness: function () {
    const { name, phone, project_id } = this.data;
    if (!name) return wx.showToast({
      title: '请输入接单人姓名',
      icon: 'none',
    })
    if (!phone) return wx.showToast({
      title: '请输入接单人联系方式',
      icon: 'none',
    })
    if (!/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(phone)) {
      return wx.showToast({
        title: '请输入正确的联系方式',
        icon: 'none',
      })
    }
    http('/user/projectOrder', 'post', this.data)
    .then((data) => {
      wx.showToast({
        title: '接单成功',
        icon: 'none',
      })
      this.setData({
        isTake: false,
        param: {
          page: 1,
          pagesize: 10
        }
      })
      this.getList();
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg || '系统出错,请稍后再试',
        icon: 'none',
      })
    })
  },
  handleCancel: function (e) {
    const type = e.currentTarget.dataset;
    console.log(type);
    this.setData({
      isChoose: type == "isChoose",
      isTake: type == "isTake"
    })
    
  },
  handleChoose: function (e) {
    const { id } = e.currentTarget.dataset;
    this.setData({
      project_id: id,
    });
    http('/user/myPeople', 'post', {
      project_id: id
    })
    .then((data) => {
      this.setData({
        isChoose: true,
        choose: data,
      });
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg || '系统出错，请稍后再试',
        icon: 'none',
      });
    })
  },
  handleChoosePerson(e) {
    const { id } = e.currentTarget.dataset;
    const data = this.data;
    const page = this;
    http('/user/myChecked', 'post', {
      ...data,
      ...{ checked_id: id},
    })
    .then((data) => {
      page.setData({
        isChoose: false,
        myPublish: [],
        param: {
          page: 1,
          pagesize: 10
        }
      })
      page.getMyPublish();
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg || '系统出错，请稍后再试',
        icon: 'none',
      });
    })
  },
  goLaunchPage: function () {
    wx.navigateTo({
      url: './launchDemand/index',
    })
  },
  onLoad: function () {
    const page = this;
    wx.login({
      success(code) {
        http('/user/isLogin', 'post', { code: code.code })
        .then((data) => {
          wx.setStorageSync('openid', data.openid);
          wx.setStorageSync('level', data.level);
          page.getList();
        })
        .catch((e) => {
          if (e.code == 1001) {
            wx.setStorageSync('openid', e.data);
            page.setData({
              showDialog: true,
            })
          }
        })
      },
    })
  },
  getList() {
    const { param, total, list } = this.data;
    const page = this;
    wx.showLoading({
      title: '加载中',
    });
    http('/user/projectList', 'post', param)
    .then((data) => {
      ++param.page;
      console.log(util.formatTime);
      data.list.forEach((item) => {
        item.time = util.formatTime(new Date(item.time * 1000))
      })
      page.setData({
        list: [...list, ...data.list],
        total: data.count,
        param
      })
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg || '系统出错，请稍后再试',
        icon: 'none'
      })
    })
  },
  getMyList() {
    wx.showLoading({
      title: '加载中',
    });
    const { param, total, myList } = this.data;
    const page = this;
    http('/user/myOrder', 'post', param)
    .then((data) => {
      ++param.page;
      data.list.forEach((item) => {
        item.time = util.formatTime(new Date(item.time * 1000))
      })
      page.setData({
        myList: [...myList, ...data.list],
        total: data.count,
        param
      })
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg,
        icon: 'none'
      })
    })
  },
  getMyPublish() {
    wx.showLoading({
      title: '加载中',
    });
    const { param, total, myPublish } = this.data;
    const page = this;
    http('/user/myPublish', 'post', param)
    .then((data) => {
      ++param.page;
      data.list.forEach((item) => {
        item.time = util.formatTime(new Date(item.time * 1000))
      })
      page.setData({
        myPublish: [...myPublish, ...data.list],
        total: data.count,
        param
      })
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg,
        icon: 'none'
      })
    })
  },
  handleDialogConfirm() {
    const { nickname } = this.data;
    const page = this;
    if (!nickname) {
      return wx.showToast({
        title: '姓名不得为空',
        icon: 'none',
      })
    }
    http('/user/register', 'post', {
      nickname,
      openid: wx.getStorageSync('openid')
    })
    .then((data) => {
      page.setData({
        showDialog: false,
      })
      wx.setStorageSync('level', data.level);
    })
    .catch((e) => {
      wx.showToast({
        title: e.msg,
        icon: 'none'
      })
    })
  },
  handleDialogInput(e) {
    const { value } = e.detail;
    this.setData({
      nickname: value,
    })
  },
  onReachBottom() {
    const { param, total } = this.data;
    if ((param.page - 1) * param.pagesize < total) {
      if (this.data.currentTab == 1) {
        this.getMyList();
      } else if (this.data.currentTab == 2) {
        this.getMyPublish();
      } else {
        this.getList()
      }
    }
  }
})
